package vip.web3.admin.sys.service.impl.biz;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import vip.web3.admin.common.utils.Query;
import vip.web3.admin.sys.dao.biz.AOrderDao;
import vip.web3.admin.sys.dao.biz.ARecordDao;
import vip.web3.admin.sys.dao.biz.AUserDao;
import vip.web3.admin.sys.entity.biz.AOrderEntity;
import vip.web3.admin.sys.entity.biz.ARecordEntity;
import vip.web3.admin.sys.entity.biz.AUserEntity;
import vip.web3.admin.sys.entity.biz.AUserWalletEntity;
import vip.web3.admin.sys.form.user.AuthAuditForm;
import vip.web3.admin.sys.form.user.AuthPageForm;
import vip.web3.admin.sys.form.user.UserPageForm;
import vip.web3.admin.sys.mapper.AUserMapper;
import vip.web3.admin.sys.service.biz.IARecordService;
import vip.web3.admin.sys.service.biz.IAUserService;
import vip.web3.admin.sys.service.biz.IAUserWalletService;
import vip.web3.admin.sys.service.sys.SysConfigService;
import vip.web3.admin.sys.vo.biz.AuthVo;
import vip.web3.admin.sys.vo.biz.export.ExUserVo;
import vip.web3.common.enums.AssetEnum;
import vip.web3.common.enums.OperateEnum;
import vip.web3.common.enums.user.UserAuthEnum;
import vip.web3.common.exception.RRException;
import vip.web3.common.utils.DateUtils;
import vip.web3.common.utils.PageUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * aut-horlilei
 */
@Slf4j
@Service
public class AUserServiceImpl extends ServiceImpl<AUserDao, AUserEntity> implements IAUserService {

    @Resource
    private SysConfigService sysConfigService;

    @Resource
    private IAUserWalletService iAUserWalletService;

    @Resource
    private IARecordService iARecordService;

    @Resource
    private AOrderDao aOrderDao;
    @Resource
    private AUserDao aUserDao;
    @Resource
    private ARecordDao aRecordDao;

    @SuppressWarnings("all")
    @Override
    public PageUtils listUser(Map<String, Object> params, UserPageForm form) {

        IPage<AUserEntity> page = this.page(
                new Query<AUserEntity>().getPage(params),
                this.<AUserEntity>lambdaQuery()
                        .ge(Objects.nonNull(form.getStartTime()), AUserEntity::getCreateTime, form.getStartTime())
                        .le(Objects.nonNull(form.getEndTime()), AUserEntity::getCreateTime, form.getEndTime())
                        .eq(Objects.nonNull(form.getPath()), AUserEntity::getPath, form.getPath())
                        .eq(Objects.nonNull(form.getPath2()), AUserEntity::getPath2, form.getPath2())
                        .eq(Objects.nonNull(form.getPath3()), AUserEntity::getPath3, form.getPath3())
                        .eq(Objects.nonNull(form.getAuthStatus()), AUserEntity::getAuthStatus, form.getAuthStatus())
                        .and(StringUtils.isNotBlank(form.getKeyWord()),
                                wrapper -> wrapper.like(AUserEntity::getMobile, form.getKeyWord())
                                        .or()
                                        .like(AUserEntity::getNick, form.getKeyWord())
                                        .or()
                                        .like(AUserEntity::getId, form.getKeyWord())
                        ).getWrapper()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ExUserVo> export(Map<String, Object> params, UserPageForm form) {
        List<AUserEntity> list = this.lambdaQuery()
                .ge(Objects.nonNull(form.getStartTime()), AUserEntity::getCreateTime, form.getStartTime())
                .le(Objects.nonNull(form.getEndTime()), AUserEntity::getCreateTime, form.getEndTime())
                .eq(Objects.nonNull(form.getPath()), AUserEntity::getPath, form.getPath())
                .eq(Objects.nonNull(form.getPath2()), AUserEntity::getPath2, form.getPath2())
                .eq(Objects.nonNull(form.getPath3()), AUserEntity::getPath3, form.getPath3())
                .eq(Objects.nonNull(form.getAuthStatus()), AUserEntity::getAuthStatus, form.getAuthStatus())
                .and(StringUtils.isNotBlank(form.getKeyWord()),
                        wrapper -> wrapper.like(AUserEntity::getMobile, form.getKeyWord())
                                .or()
                                .like(AUserEntity::getNick, form.getKeyWord())
                                .or()
                                .like(AUserEntity::getId, form.getKeyWord())
                ).list();
        return AUserMapper.INSTANCE.toExUserVos(list);
    }

    @Override
    public AuthVo getAuthInfo(Integer id) {
        AuthVo authInfo = this.baseMapper.getAuthInfo(id);
        authInfo.setAuthStatusDesc(UserAuthEnum.get(authInfo.getAuthStatus()).getDesc());
        return authInfo;
    }

    @Override
    public void authAudit(AuthAuditForm form) {
        this.lambdaUpdate()
                .set(AUserEntity::getAuthStatus, form.getAuthStatus())
                .eq(AUserEntity::getId, form.getId())
                .update();
    }

    @Override
    public void edit(AUserEntity form) {
        if (Objects.isNull(form) || form.getId() == null) {
            throw new RRException("id不能为空!!", -1);
        }
        this.baseMapper.updateById(form);
    }

    @Override
    public PageUtils pageAuthInfo(Map<String, Object> params, AuthPageForm form) {
        IPage<AuthVo> page = this.baseMapper.pageAuthInfo(new Query<AuthVo>().getPage(params), params, form);
        page.getRecords().forEach(e -> e.setAuthStatusDesc(UserAuthEnum.get(e.getAuthStatus()).getDesc()));
        return new PageUtils(page);
    }


    @Override
    public void teamReward() {
        QueryWrapper<AUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("active", 1);
        List<AUserEntity> users = baseMapper.selectList(queryWrapper);

        String team1_ = sysConfigService.getValue("alloy:team:level:count:1");
        int team1Count = Integer.parseInt(team1_.split("_")[0]);
        BigDecimal team1Amount = new BigDecimal(team1_.split("_")[1]);

        String team2_ = sysConfigService.getValue("alloy:team:level:count:2");
        int team2Count = Integer.parseInt(team2_.split("_")[0]);
        BigDecimal team2Amount = new BigDecimal(team2_.split("_")[1]);

        String team3_ = sysConfigService.getValue("alloy:team:level:count:3");
        int team3Count = Integer.parseInt(team3_.split("_")[0]);
        BigDecimal team3Amount = new BigDecimal(team3_.split("_")[1]);

        String team4_ = sysConfigService.getValue("alloy:team:level:count:4");
        int team4Count = Integer.parseInt(team4_.split("_")[0]);
        BigDecimal team4Amount = new BigDecimal(team4_.split("_")[1]);

        String team1Rate1_ = sysConfigService.getValue("alloy:team:level:1:rate:1");
        String team1Rate2_ = sysConfigService.getValue("alloy:team:level:1:rate:3");
        String team1Rate3_ = sysConfigService.getValue("alloy:team:level:1:rate:6");
        String team1Rate4_ = sysConfigService.getValue("alloy:team:level:1:rate:10");

        BigDecimal team1Rate11 = new BigDecimal(team1Rate1_);
        BigDecimal team1Rate13 = new BigDecimal(team1Rate2_);
        BigDecimal team1Rate16 = new BigDecimal(team1Rate3_);
        BigDecimal team1Rate110 = new BigDecimal(team1Rate4_);

        String team2Rate1_ = sysConfigService.getValue("alloy:team:level:2:rate:1");
        String team2Rate2_ = sysConfigService.getValue("alloy:team:level:2:rate:3");
        String team2Rate3_ = sysConfigService.getValue("alloy:team:level:2:rate:6");
        String team2Rate4_ = sysConfigService.getValue("alloy:team:level:2:rate:10");

        BigDecimal team1Rate21 = new BigDecimal(team2Rate1_);
        BigDecimal team1Rate23 = new BigDecimal(team2Rate2_);
        BigDecimal team1Rate26 = new BigDecimal(team2Rate3_);
        BigDecimal team1Rate210 = new BigDecimal(team2Rate4_);

        String team3Rate1_ = sysConfigService.getValue("alloy:team:level:3:rate:1");
        String team3Rate2_ = sysConfigService.getValue("alloy:team:level:3:rate:3");
        String team3Rate3_ = sysConfigService.getValue("alloy:team:level:3:rate:6");
        String team3Rate4_ = sysConfigService.getValue("alloy:team:level:3:rate:10");

        BigDecimal team1Rate31 = new BigDecimal(team3Rate1_);
        BigDecimal team1Rate33 = new BigDecimal(team3Rate2_);
        BigDecimal team1Rate36 = new BigDecimal(team3Rate3_);
        BigDecimal team1Rate310 = new BigDecimal(team3Rate4_);

        String team4Rate1_ = sysConfigService.getValue("alloy:team:level:4:rate:1");
        String team4Rate2_ = sysConfigService.getValue("alloy:team:level:4:rate:3");
        String team4Rate3_ = sysConfigService.getValue("alloy:team:level:4:rate:6");
        String team4Rate4_ = sysConfigService.getValue("alloy:team:level:4:rate:10");

        BigDecimal team1Rate41 = new BigDecimal(team4Rate1_);
        BigDecimal team1Rate43 = new BigDecimal(team4Rate2_);
        BigDecimal team1Rate46 = new BigDecimal(team4Rate3_);
        BigDecimal team1Rate410 = new BigDecimal(team4Rate4_);

        String personal1_ = sysConfigService.getValue("alloy:personal:level:limit:1");
        BigDecimal personal1limit = new BigDecimal(personal1_.split("_")[0]);
        BigDecimal personal1rate = new BigDecimal(personal1_.split("_")[1]);

        String personal2_ = sysConfigService.getValue("alloy:personal:level:limit:2");
        BigDecimal personal2limit = new BigDecimal(personal2_.split("_")[0]);
        BigDecimal personal2rate = new BigDecimal(personal2_.split("_")[1]);

        String personal3_ = sysConfigService.getValue("alloy:personal:level:limit:3");
        BigDecimal personal3limit = new BigDecimal(personal3_.split("_")[0]);
        BigDecimal personal3rate = new BigDecimal(personal3_.split("_")[1]);

        String personal4_ = sysConfigService.getValue("alloy:personal:level:limit:4");
        BigDecimal personal4limit = new BigDecimal(personal4_.split("_")[0]);
        BigDecimal personal4rate = new BigDecimal(personal4_.split("_")[1]);

        String personal5_ = sysConfigService.getValue("alloy:personal:level:limit:5");
        BigDecimal personal5limit = new BigDecimal(personal5_.split("_")[0]);
        BigDecimal personal5rate = new BigDecimal(personal5_.split("_")[1]);

        List<BigDecimal> limitList = new ArrayList<>();
        limitList.add(personal5limit);
        limitList.add(personal4limit);
        limitList.add(personal3limit);
        limitList.add(personal2limit);
        limitList.add(personal1limit);

        List<BigDecimal> rateList = new ArrayList<>();
        rateList.add(personal5rate);
        rateList.add(personal4rate);
        rateList.add(personal3rate);
        rateList.add(personal2rate);
        rateList.add(personal1rate);

        if (!CollectionUtils.isEmpty(users)) {
            for (AUserEntity user : users) {
                try {
                    Date activeEnd = user.getActiveEnd();
                    Date now = new Date();

                    if (DateUtils.format(activeEnd, DateUtils.DATE_PATTERN)
                            .equals(DateUtils.format(now, DateUtils.DATE_PATTERN))) {
                        // 团队等级奖励
                        QueryWrapper<AOrderEntity> query = new QueryWrapper<>();
                        query.ne("pro_id", 1);
                        query.ge("create_date", user.getActiveStart());
                        query.lt("create_date", activeEnd);
                        query.inSql("user_id",
                                "select id from a_user where path = " + user.getId() + " or path2 = " + user.getId() + " or path3 = " + user.getId());
                        List<AOrderEntity> orders = aOrderDao.selectList(query);
                        int countOrder = 0; // 团队单数
                        BigDecimal totalPerformance = BigDecimal.ZERO; // 团队业绩
                        if (!CollectionUtils.isEmpty(orders)) {
                            countOrder = orders.size();
                            totalPerformance = orders.stream().map(AOrderEntity::getAmountCny).reduce(BigDecimal.ZERO, BigDecimal::add);
                        }
                        BigDecimal reward = BigDecimal.ZERO;
                        int teamLevel = user.getTeamLevel();
                        int teamSustain = user.getTeamSustain();
                        if (countOrder >= team4Count && totalPerformance.compareTo(team4Amount) >= 0) {
                            user.setTeamLevel(4);
                            user.setTeamName("合金战略合伙人");
                            if (teamLevel == 4) {
                                user.setTeamSustain(teamSustain + 1);
                            } else {
                                user.setTeamSustain(1);
                            }
                            int sustain = user.getTeamSustain();
                            BigDecimal rate = BigDecimal.ZERO;
                            if (sustain >= 10) {
                                // 连续10个月
                                rate = team1Rate410;
                            } else if (sustain >= 6) {
                                // 连续6个月
                                rate = team1Rate46;
                            } else if (sustain >= 3) {
                                // 连续3个月
                                rate = team1Rate43;
                            } else {
                                // 连续1个月
                                rate = team1Rate41;
                            }
                            reward = totalPerformance.multiply(rate);
                        } else if (countOrder >= team3Count && totalPerformance.compareTo(team3Amount) >= 0) {
                            user.setTeamLevel(3);
                            user.setTeamName("合金顶创者");
                            if (teamLevel == 3) {
                                user.setTeamSustain(teamSustain + 1);
                            } else {
                                user.setTeamSustain(1);
                            }
                            Integer sustain = user.getTeamSustain();
                            BigDecimal rate = BigDecimal.ZERO;
                            if (sustain >= 10) {
                                // 连续10个月
                                rate = team1Rate310;
                            } else if (sustain >= 6) {
                                // 连续6个月
                                rate = team1Rate36;
                            } else if (sustain >= 3) {
                                // 连续3个月
                                rate = team1Rate33;
                            } else {
                                // 连续1个月
                                rate = team1Rate31;
                            }
                            reward = totalPerformance.multiply(rate);
                        } else if (countOrder >= team2Count && totalPerformance.compareTo(team2Amount) >= 0) {
                            user.setTeamLevel(2);
                            user.setTeamName("合金中创者");
                            if (teamLevel == 2) {
                                user.setTeamSustain(teamSustain + 1);
                            } else {
                                user.setTeamSustain(1);
                            }
                            Integer sustain = user.getTeamSustain();
                            BigDecimal rate = BigDecimal.ZERO;
                            if (sustain >= 10) {
                                // 连续10个月
                                rate = team1Rate210;
                            } else if (sustain >= 6) {
                                // 连续6个月
                                rate = team1Rate26;
                            } else if (sustain >= 3) {
                                // 连续3个月
                                rate = team1Rate23;
                            } else {
                                // 连续1个月
                                rate = team1Rate21;
                            }
                            reward = totalPerformance.multiply(rate);
                        } else if (countOrder >= team1Count && totalPerformance.compareTo(team1Amount) >= 0) {
                            user.setTeamLevel(1);
                            user.setTeamName("合金初创者");
                            if (teamLevel == 1) {
                                user.setTeamSustain(teamSustain + 1);
                            } else {
                                user.setTeamSustain(1);
                            }
                            Integer sustain = user.getTeamSustain();
                            BigDecimal rate = BigDecimal.ZERO;
                            if (sustain >= 10) {
                                // 连续10个月
                                rate = team1Rate110;
                            } else if (sustain >= 6) {
                                // 连续6个月
                                rate = team1Rate16;
                            } else if (sustain >= 3) {
                                // 连续3个月
                                rate = team1Rate13;
                            } else {
                                // 连续1个月
                                rate = team1Rate11;
                            }
                            reward = totalPerformance.multiply(rate);
                        } else {
                            user.setTeamLevel(0);
                            user.setTeamName("合金VIP");
                            user.setTeamSustain(0);
                        }
                        baseMapper.updateById(user);
                        if (reward.compareTo(BigDecimal.ZERO) > 0) {
                            // 发放奖励 操作钱包
                            AUserWalletEntity path1Wallet = iAUserWalletService.findByUserId(user.getId());
                            iAUserWalletService.updateWallet(path1Wallet, AssetEnum.TYPE3.getCode(), reward, BigDecimal.ZERO);
                            // 添加记录
                            iARecordService.addRewardRecord(user.getId(),
                                    user.getMobile(),
                                    OperateEnum.TYPE7.getType(),
                                    OperateEnum.TYPE7.getMark(),
                                    user.getTeamName() + " :" + OperateEnum.TYPE7.getDesc(),
                                    reward,
                                    BigDecimal.ZERO,
                                    OperateEnum.OPERATE_TYPE1.getType(),
                                    "", AssetEnum.STATUS1.getCode()
                            );
                        }
                        // 个人累计业绩奖励
                        personalReward(user, limitList, rateList);
                    }

                } catch (Exception ex) {
                    log.error("等级分红异常...");
                }
            }
        }
    }

    @Override
    public void personalReward(AUserEntity user, List<BigDecimal> limitList, List<BigDecimal> rateList) {
        // 个人累计业绩奖励
        BigDecimal performance = aOrderDao.selectTotalOrder(user.getId(), user.getActiveStart(), user.getActiveEnd());
        int personalLevel = user.getPersonalLevel();
        // 只升不降
        if (performance.compareTo(limitList.get(0)) >= 0) {
            if (personalLevel < 5) {
                user.setPersonalLevel(5);
                user.setPersonalName("战略合金玩家");
            }
        } else if (performance.compareTo(limitList.get(1)) >= 0) {
            if (personalLevel < 4) {
                user.setPersonalLevel(4);
                user.setPersonalName("大师合金玩家");
            }
        } else if (performance.compareTo(limitList.get(2)) >= 0) {
            if (personalLevel < 3) {
                user.setPersonalLevel(3);
                user.setPersonalName("专业合金玩家");
            }
        } else if (performance.compareTo(limitList.get(3)) >= 0) {
            if (personalLevel < 2) {
                user.setPersonalLevel(2);
                user.setPersonalName("资深合金玩家");
            }
        } else if (performance.compareTo(limitList.get(4)) >= 0) {
            if (personalLevel < 1) {
                user.setPersonalLevel(1);
                user.setPersonalName("初级合金玩家");
            }
        }

        // 更新等级后, 计算奖励比例
        BigDecimal reward = BigDecimal.ZERO;
        int newLevel = user.getPersonalLevel();
        if (newLevel >= 5) {
            reward = performance.multiply(rateList.get(0));
        } else if (newLevel >= 4) {
            reward = performance.multiply(rateList.get(1));
        } else if (newLevel >= 3) {
            reward = performance.multiply(rateList.get(2));
        } else if (newLevel >= 2) {
            reward = performance.multiply(rateList.get(3));
        } else if (newLevel >= 1) {
            reward = performance.multiply(rateList.get(4));
        }

        if (reward.compareTo(BigDecimal.ZERO) > 0) {
            // 发放奖励 操作钱包
            AUserWalletEntity path1Wallet = iAUserWalletService.findByUserId(user.getId());
            iAUserWalletService.updateWallet(path1Wallet, AssetEnum.TYPE3.getCode(), reward, BigDecimal.ZERO);
            // 添加记录
            iARecordService.addRewardRecord(user.getId(),
                    user.getMobile(),
                    OperateEnum.TYPE8.getType(),
                    OperateEnum.TYPE8.getMark(),
                    user.getPersonalName() + " :" + OperateEnum.TYPE8.getDesc(),
                    reward,
                    BigDecimal.ZERO,
                    OperateEnum.OPERATE_TYPE1.getType(),
                    "", AssetEnum.STATUS1.getCode()
            );
            // 更新执行时间
            String format = DateUtils.format(new Date(), DateUtils.DATE_PATTERN);
            try {
                Date parse = DateUtils.parse(format + " 00:00:00", DateUtils.DATE_TIME_PATTERN);
                user.setActiveStart(parse);
                user.setActiveEnd(DateUtils.addDateDays(parse, 30));
            } catch (Exception ex) {
            }
            baseMapper.updateById(user);
        }
    }

    @Override
    public void quarterReward(Integer userId) {
        AUserEntity user = aUserDao.selectById(userId);
        String quarter1_ = sysConfigService.getValue("alloy:quarter:level:1");
        String quarter2_ = sysConfigService.getValue("alloy:quarter:level:2");
        String quarter3_ = sysConfigService.getValue("alloy:quarter:level:3");
        String quarter4_ = sysConfigService.getValue("alloy:quarter:level:4");
        String quarter5_ = sysConfigService.getValue("alloy:quarter:level:5");

        BigDecimal quarter1Team = new BigDecimal(quarter1_.split("_")[0]);
        BigDecimal quarter1User = new BigDecimal(quarter1_.split("_")[1]);
        BigDecimal quarter1Order = new BigDecimal(quarter1_.split("_")[2]);

        BigDecimal quarter2Team = new BigDecimal(quarter2_.split("_")[0]);
        BigDecimal quarter2User = new BigDecimal(quarter2_.split("_")[1]);
        BigDecimal quarter2Order = new BigDecimal(quarter2_.split("_")[2]);

        BigDecimal quarter3Team = new BigDecimal(quarter3_.split("_")[0]);
        BigDecimal quarter3User = new BigDecimal(quarter3_.split("_")[1]);
        BigDecimal quarter3Order = new BigDecimal(quarter3_.split("_")[2]);

        BigDecimal quarter4Team = new BigDecimal(quarter4_.split("_")[0]);
        BigDecimal quarter4User = new BigDecimal(quarter4_.split("_")[1]);
        BigDecimal quarter4Order = new BigDecimal(quarter4_.split("_")[2]);

        BigDecimal quarter5Team = new BigDecimal(quarter5_.split("_")[0]);
        BigDecimal quarter5User = new BigDecimal(quarter5_.split("_")[1]);
        BigDecimal quarter5Order = new BigDecimal(quarter5_.split("_")[2]);

        String rate1_ = sysConfigService.getValue("alloy:quarter:rate:1");
        String rate2_ = sysConfigService.getValue("alloy:quarter:rate:2");
        String rate3_ = sysConfigService.getValue("alloy:quarter:rate:3");
        String rate4_ = sysConfigService.getValue("alloy:quarter:rate:4");
        String rate5_ = sysConfigService.getValue("alloy:quarter:rate:5");

        BigDecimal rate1 = new BigDecimal(rate1_);
        BigDecimal rate2 = new BigDecimal(rate2_);
        BigDecimal rate3 = new BigDecimal(rate3_);
        BigDecimal rate4 = new BigDecimal(rate4_);
        BigDecimal rate5 = new BigDecimal(rate5_);


        // 团队等级奖励
        QueryWrapper<AOrderEntity> query = new QueryWrapper<>();
        query.inSql("user_id",
                "select id from a_user where path = " + user.getId() + " or path2 = " + user.getId() + " or path3 = " + user.getId());
        List<AOrderEntity> orders = aOrderDao.selectList(query);
        BigDecimal totalPerformance = BigDecimal.ZERO; // 团队业绩
        if (!CollectionUtils.isEmpty(orders)) {
            totalPerformance = orders.stream().map(AOrderEntity::getAmountCny).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        // 个人业绩
        BigDecimal performance = aOrderDao.selectTotalOrderUserId(user.getId());
        if (performance.compareTo(new BigDecimal(1)) < 0) {
            performance = aOrderDao.selectTotalOrderUsdt(user.getId());
        }

        BigDecimal reward = BigDecimal.ZERO;

        if ((totalPerformance.compareTo(quarter5Team) >= 0 && performance.compareTo(quarter5User) >= 0) ||
                performance.compareTo(quarter5Order) >= 0) {
            user.setQuarterLevel(5);
            user.setQuarterName("合金原料5号");
            reward = rate5;
        } else if ((totalPerformance.compareTo(quarter4Team) >= 0 && performance.compareTo(quarter4User) >= 0) ||
                performance.compareTo(quarter4Order) >= 0) {
            user.setQuarterLevel(4);
            user.setQuarterName("合金原料4号");
            reward = rate4;
        } else if ((totalPerformance.compareTo(quarter3Team) >= 0 && performance.compareTo(quarter3User) >= 0) ||
                performance.compareTo(quarter3Order) >= 0) {
            user.setQuarterLevel(3);
            user.setQuarterName("合金原料3号");
            reward = rate3;
        } else if ((totalPerformance.compareTo(quarter2Team) >= 0 && performance.compareTo(quarter2User) >= 0) ||
                performance.compareTo(quarter2Order) >= 0) {
            user.setQuarterLevel(2);
            user.setQuarterName("合金原料2号");
            reward = rate2;
        } else if ((totalPerformance.compareTo(quarter1Team) >= 0 && performance.compareTo(quarter1User) >= 0) ||
                performance.compareTo(quarter1Order) >= 0) {
            user.setQuarterLevel(1);
            user.setQuarterName("合金原料1号");
            reward = rate1;
        }
        log.info("开始更新用户QuarterName" + JSONObject.toJSONString(user));
        this.saveOrUpdate(user);

//        if (reward.compareTo(BigDecimal.ZERO) > 0) {
//            LambdaQueryWrapper<ARecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            lambdaQueryWrapper.eq(ARecordEntity::getType, OperateEnum.TYPE9.getType());
//            lambdaQueryWrapper.eq(ARecordEntity::getAmount, reward);
//            lambdaQueryWrapper.eq(ARecordEntity::getUserId, user.getId());
//            List<ARecordEntity> aRecordEntities = aRecordDao.selectList(lambdaQueryWrapper);
//            // 发放奖励 操作钱包
//            if (CollectionUtils.isEmpty(aRecordEntities)) {
//                AUserWalletEntity path1Wallet = iAUserWalletService.findByUserId(user.getId());
//                iAUserWalletService.updateWallet(path1Wallet, AssetEnum.TYPE3.getCode(), reward, BigDecimal.ZERO);
//                // 添加记录
//                iARecordService.addRewardRecord(user.getId(),
//                        user.getMobile(),
//                        OperateEnum.TYPE9.getType(),
//                        OperateEnum.TYPE9.getMark(),
//                        user.getQuarterName() + " :" + OperateEnum.TYPE9.getDesc(),
//                        reward,
//                        BigDecimal.ZERO,
//                        OperateEnum.OPERATE_TYPE1.getType(),
//                        "", AssetEnum.STATUS1.getCode()
//                );
//            }
//        }

    }


    @Override
    public void passAuth(List<Integer> ids) {
        this.baseMapper.batchSetAuthStatus(ids, UserAuthEnum.AUTH_PASS.getCode());
    }

    @Override
    public void statusOff(List<Integer> ids) {
        this.baseMapper.batchSetStatus(ids, UserAuthEnum.NO_AUTH.getCode());
    }

    public static void main(String[] args) {
        Date now = new Date();
        String format = null;
        String end = null;
        int a = 1;
        if (a == 1 || "01-01".equals(DateUtils.format(now, DateUtils.MM_DD))) {
            Date date = DateUtils.addDateYears(now, -1);
            format = DateUtils.format(date, DateUtils.yyyy);
            format = format + "-09-01 00:00:00";
            end = "2024-01-01 00:00:00";

        } else if (a == 2 || "03-01".equals(DateUtils.format(now, DateUtils.MM_DD))) {
            format = DateUtils.format(now, DateUtils.yyyy);
            format = format + "-01-01 00:00:00";
            end = "2024-03-01 00:00:00";
        } else if (a == 3 || "06-01".equals(DateUtils.format(now, DateUtils.MM_DD))) {
            format = DateUtils.format(now, DateUtils.yyyy);
            format = format + "-03-01 00:00:00";
            end = "2024-06-01 00:00:00";
        } else if (a == 4 || "09-01".equals(DateUtils.format(now, DateUtils.MM_DD))) {
            format = DateUtils.format(now, DateUtils.yyyy);
            format = format + "-06-01 00:00:00";
            end = "2024-09-01 00:00:00";
        }
        System.out.println(DateUtils.format(now, DateUtils.MM_DD));
        if (format != null) {
//            Date start = DateUtils.stringToDate(format, DateUtils.DATE_TIME_PATTERN);
//            Date end = DateUtils.stringToDate(DateUtils.format(now, DateUtils.DATE_PATTERN) + " 00:00:00", DateUtils.DATE_TIME_PATTERN);
            System.out.println(format);
            System.out.println(end);
            System.out.println(DateUtils.format(now, DateUtils.DATE_PATTERN) + " 00:00:00");
        }
    }

}
